<!--
* description: 调拨计划创建
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicUnFormFields="unFormField"
      :dynamicIsShowSelect="true"
      :dynamicIsColumnDrop="true"
      :dynamicIsInitTable="true"
      :callbackObj="callbackObj"
    >
      <template slot="controlBtn" slot-scope="scope">
        <el-link
          type="primary"
          class="el-icon-edit"
          :underline="false"
          @click="edit(scope.row.index)"
          title="编辑"
          style="font-size:12px;"
        >编辑</el-link>
      </template>
    </one-table-template>
    <edit
      :dynamicEditRowData="editRowData"
      :popupsVisible="editPopupsVisible"
      :key="editPopupsKey"
      :popupsState="editPopupsState"
      @close="close"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { wmsApis } from "@/api/wms/allocation/api";
import OneTableTemplate from "@/components/wms/templates/oneTable";
import request from "@/utils/request";
import store from "@/store";
export default {
  name: "packingMaterialOptimization",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate,
    Edit: () => import("./edit")
  },
  watch: {
    $route(val) {
      if (
        this.formField.pageId !== "" &&
        this.$route.query.pageId !== undefined &&
        this.$route.query.pageId !== this.formField.pageId
      ) {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
        this.queryTable();
      }
    }
  },
  created() {
    if (this.$route.query != null) {
      // 判断传入query参数，初始化数据
      if (this.$route.query.pageId != null && this.$route.query.pageId !== "") {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
      }
    }
  },
  mounted() {
    this.querySelect();
  },
  data() {
    return {
      // 网格查询API配置对象
      apiConfig: wmsApis.queryAllocation,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询" //"查询"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.add(),
          name: "add",
          position: "left",
          text: "新增"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置"
        },
        {
          compKey: "btnKey5",
          type: "",
          size: "small",
          clickEvent: () => this.submissionUp(),
          name: "submissionUp",
          position: "left",
          text: "提交"
        },
        {
          compKey: "btnKey5",
          type: "",
          size: "small",
          clickEvent: () => this.cancel(),
          name: "cancel",
          position: "left",
          text: "取消"
        },
         {
          compKey: "btnKey6",
          type: "",
          size: "small",
          name: "importTemplateDownload",
          position: "left",
          clickEvent: () => this.downloadTemplate(),
          text: "模板下载",
        },
          {
          compKey: "btnKey7",
          text: "导入",
          category: "ve",
          codeField: "fileChannel",
          position: "upload",
          isMust: true,
        },
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "源仓库",
          codeField: "SOURCE_WAREHOUSE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          options: []
        },
        {
          compKey: "compKey2",
          labelName: "目标仓库",
          codeField: "TARGET_WAREHOUSE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          options: []
        },
        {
          compKey: "compKey3",
          labelName: "备件编号",
          codeField: "PART_NO",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey4",
          labelName: "计划状态",
          codeField: "ALLOCATION_STATUS",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          options: []
        },
        {
          compKey: "compKey5",
          labelName: "调拨计划",
          codeField: "ALLOCATION_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "controlBtn",
          label: "操作",
          codeField: "controlBtn",
          align: "center",
          isSlot: true
        },
        {
          prop: "ALLOCATION_CODE",
          label: "调拨计划",
          align: "center"
        },
        {
          prop: "SOURCE_WAREHOUSE_NAME",
          label: "源仓库",
          align: "center"
        },
        {
          prop: "TARGET_WAREHOUSE_NAME",
          label: "目标仓库",
          align: "center"
        },
        {
          prop: "PART_NO",
          label: "备件编号",
          align: "center"
        },
        {
          prop: "PART_NAME",
          label: "备件名称",
          align: "center"
        },
        {
          prop: "ALLOCATION_QTY",
          label: "调拨数量",
          align: "center"
        },
        {
          prop: "SOURCE_QTY",
          label: "源仓库可用库存",
          align: "center"
        },
        {
          prop: "TARGET_QTY",
          label: "目标仓库可用库存",
          align: "center"
        },
        {
          prop: "ALLOCATION_STATUS_NAME",
          label: "计划状态",
          align: "center"
        },
        {
          prop: "REJECT_REASON",
          label: "驳回原因",
          align: "center"
        }
      ],
      //表单查询数据
      formField: {
        SOURCE_WAREHOUSE: "",
        TARGET_WAREHOUSE: "",
        PART_NO: "",
        ALLOCATION_STATUS: "",
        ALLOCATION_CODE: "",
        PAGE_STATUS: "1"
      },
      unFormField: {},
      // 回调函数
      callbackObj: {
        //  当选择项发生变化时会触发该事件
        onCheckSelectChange: selection => {
          this.checkTableList = selection;
        }
      },
      // 选中的表格数据
      checkTableList: []
    };
  },
  methods: {
    cancel() {
      if (this.checkTableList.length > 0) {
        this.$confirm("确定要取消吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        })
          .then(() => {
            request({
              url: "/mp/wm/vl/warehouse/allocation/cancelAllocation.do",
              method: "post",
              data: {
                cancelAllocation: this.checkTableList
              }
            }).then(res => {
              if (res.result === "1") {
                this.queryTable();
                this.$message({
                  message: "取消成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.debugMsg ? res.debugMsg : res.msg,
                  type: "warn"
                });
              }
            });
          })
          .catch(err => {
            console.info(err);
          });
      } else {
        this.$message({
          message: "请选择一条数据"
        });
      }
    },
    submissionUp() {
      if (this.checkTableList.length > 0) {
        this.$confirm("确定要提交吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        })
          .then(() => {
            request({
              url: "/mp/wm/vl/warehouse/allocation/submitAllocation.do",
              method: "post",
              data: {
                submitAllocation: this.checkTableList
              }
            }).then(res => {
              if (res.result === "1") {
                this.queryTable();
                this.$message({
                  message: "提交成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.debugMsg ? res.debugMsg : res.msg,
                  type: "warn"
                });
              }
            });
          })
          .catch(err => {
            console.info(err);
          });
      } else {
        this.$message({
          message: "请选择一条数据"
        });
      }
    },
    querySelect() {
      request({
        url: "/mp/wm/vl/warehouse/allocation/querySrcWarehouse.do",
        method: "post",
        data: {
          IS_ENABLE: "1"
        }
      }).then(res => {
        if (
          res.listRow &&
          res.listRow &&
          res.listRow.length > 0
        ) {
          res.listRow.map(item => {
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
        }
      });

      request({
        url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
        method: "post",
        data: {
          IS_ENABLE: "1"
        }
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.tableComponents[1].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          }); 
        }
      });
      

      request({
        url: "/mp/wm/vl/warehouse/allocation/queryAllocationStatusBox.do",
        method: "post",
        data: {}
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            // 筛选字段的返回值
             if (item.ALLOCATION_STATUS === "00" || item.ALLOCATION_STATUS === "03"){
                   this.tableComponents[3].options.push({
              label: item.ALLOCATION_STATUS_NAME,
              value: item.ALLOCATION_STATUS
            });
             }
        
          });
        }
      });
    },
      downloadTemplate() {
      let downloadElement = document.createElement("a");
      let href = "/wms/fileTemplate/调拨单导入模板.xlsx"; // 创建下载的链接
      downloadElement.href = href;
      document.body.appendChild(downloadElement);
      downloadElement.click(); // 点击下载
      document.body.removeChild(downloadElement); // 下载完成移除元素
    },
    //文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    handFileListChange(file, fileList) {},
    // 上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。
    beforeAvatarUpload(file) {
      if (file.size > 50 * 1024 * 1024) {
        this.$message({
          message: "文件大小不能超过50M",
          type: "warning",
        });
        return false;
      }
      let fileType = file.name.substr(file.name.lastIndexOf(".") + 1);
      if (fileType != "xls" && fileType != "xlsx") {
        this.$message({
          message: "只能导入后缀为xls,xlsx的文件！",
          type: "warning",
        });
        return false;
      }
      return true;
    },
    handleChange(param) {
      const loading = this.$loading({
        lock: true,
        text: "导入中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      const that = this;
      const url = "/mp/wm/vl/warehouse/allocation/importExcelToAllocation.do";
      that
        .$requestUpload(url, param)
        .then((response) => {
          loading.close();
          if (response.result === "1") {
            this.$message({
              message: "导入成功",
              type: "success",
            });
            that.queryTable();
          } else {
            this.$message({
              message: response.msg,
              type: "warning",
            });
          }
        })
        .catch((err) => {});
    },
  }
};
</script>
